package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.entity.UserInfo;
import com.chexiehui.jc.ui.fragment.ChangePhoneBindFragment;
import com.chexiehui.jc.ui.fragment.ChangePhoneFinishFragment;
import com.chexiehui.jc.ui.fragment.ChangePhoneShowFragment;
import com.chexiehui.jc.ui.fragment.ChangePhoneVerificationFragment;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.utils.SerializableSpTools;

import butterknife.BindView;

/**
 * @Description 修改手机号
 * @Auth SongJun
 * @Date 2019/11/28 18:48
 **/
public class ChangePhoneActivity extends BaseActivity implements
        ChangePhoneShowFragment.PageNextListener, ChangePhoneVerificationFragment.PageNextListener,ChangePhoneBindFragment.PageNextListener,
        ChangePhoneFinishFragment.PageNextListener {


    @BindView(R.id.fl_content_page)
    FrameLayout flContentPage;

    private int pageIndex;
    private UserInfo userInfo;

    @Override
    protected void beforeSetView() {
        userInfo = SerializableSpTools.getUserInfo();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_change_phone;
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("绑定手机号")
                .setOnLeftTextClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        if(pageIndex == 0){
                            onBackPressed();
                        }else if(pageIndex == 1){
                            transactToShowPage();
                        }else if(pageIndex == 2){
                            transactToVerificationPage();
                        }else if(pageIndex == 3){
                            ActivityUtils.finishAllActivities();
                            startActivity(MainActivity.class);
                        }
                    }
                });
    }

    @Override
    protected void initView(Bundle var1) {
        transactToShowPage();
    }

    private void transactToShowPage(){
        titleBar.setTitleMainText("绑定手机号");
        pageIndex = 0;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        ChangePhoneShowFragment fragment = ChangePhoneShowFragment.getInstance();
        fragment.setPageNextListener(this);
        transaction.replace(R.id.fl_content_page, fragment);
        transaction.commit();
    }

    private void transactToVerificationPage(){
        titleBar.setTitleMainText("更换手机号");
        pageIndex = 1;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        ChangePhoneVerificationFragment fragment = ChangePhoneVerificationFragment.getInstance();
        fragment.setPageNextListener(this);
        transaction.replace(R.id.fl_content_page, fragment);
        transaction.commit();
    }


    private void transactToBindPage(){
        titleBar.setTitleMainText("更换手机号");
        pageIndex = 2;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        ChangePhoneBindFragment fragment = ChangePhoneBindFragment.getInstance();
        fragment.setPageNextListener(this);
        transaction.replace(R.id.fl_content_page, fragment);
        transaction.commit();
    }

    private void transactToFinishPage(){
        titleBar.setTitleMainText("操作完成");
        pageIndex = 3;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        ChangePhoneFinishFragment fragment = ChangePhoneFinishFragment.getInstance();
        fragment.setPageNextListener(this);
        transaction.replace(R.id.fl_content_page, fragment);
        transaction.commit();
    }

    @Override
    public void onChangePhoneShowNext() {
        transactToVerificationPage();
    }

    @Override
    public void onChangePhoneVerificationNext() {
        transactToBindPage();
    }

    @Override
    public void onChangePhoneBindNext(String phone) {
        userInfo.setTel(phone);
        SerializableSpTools.putUserInfo(userInfo);
        transactToFinishPage();
    }

    @Override
    public void onChangePhoneFinishNext() {
        ActivityUtils.finishAllActivities();
        startActivity(MainActivity.class);
    }

}